package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.TreatMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Plan;
import com.itheima.service.MemberService;
import com.itheima.service.TreatService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service(interfaceClass = TreatService.class)
@Transactional
public class TreatServiceImpl implements TreatService {
    @Autowired
    private TreatMapper treatMapper;

    @Override
    public void add(Map map) {
        String fileNUmber = (String) map.get("fileNumber");
        String name = (String) map.get("name");
        String sex = (String) map.get("sex");
        String result = (String) map.get("result");
        String title = (String) map.get("title");
       String startT = (String) map.get("startTime");
       Date startTime = new Date();
       Date endTime = new Date();
        try {
            Date startTime1 = DateUtils.parseString2Date(startT);
            startTime = startTime1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        String endT = (String) map.get("endTime");
        try {
            Date endTime1 = DateUtils.parseString2Date(endT);
            endTime = endTime1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        String point = (String) map.get("point");
        Plan plan = new Plan(fileNUmber,name,sex,result,title,startTime,endTime,point);
        treatMapper.add(plan);
    }

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        //1.使用分页助手进行分页
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());

        //2.调用MemberMapper的findByCondition方法
        String queryString = pageBean.getQueryString();
        Page<Plan> page = treatMapper.findByCondition(queryString);

        //3.封装放回对象
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public Plan findPlan(String fileNumber) {
       Plan plan = treatMapper.findPlan(fileNumber);
        return plan;
    }
}
